<?

/*	ObjectSingle OBJECT
*	Methods:
*	output
*
*
*					*/


class ObjectSingle extends modTEXT {

	function __construct( $vars ) {

		$this->_addvars( $vars );
		$this->_ref();
	}
	
	
	public function construct() {
		global $DB, $Page, $Object, $Children;

		$Object = new OBJECT( $this->vars->type, (int) $_GET['id'] );
		
		if( $this->construct->children && $Object->id ) {
			
			if( $ids = $DB->reference( $this->vars->type, (int) $Object->id, $this->construct->children ))
				$Children = $DB->filter_objects( $this->construct->children, 'ID in (' . implode( ',', $ids ) . ')');
		}
		
		$Page->set_cache( "title", $Object->get( 'name' ));
		
	}
	
	
	public function output() {
		
		if( $this->action )
			$result = $this->{ "filter_" . $this->action }();

		if( $result )
			$this->addclass( $this->action );
			
		if( is_array( $result ))
			$result = "<ul>" . implode( "\n", $result ) . "</ul>";
			
		if( $result )
			return $this->parse( $result ) . $this->render_js();
		
	}
	
	
	function filter_bio() {
		global $DB, $Object;
		
		if( !$this->vars->notitle )
			$this->name = $Object->get( 'name' );
		
		$result = "<span class='georgia type'>". $DB->type_r( $Object->get( "type" )) ."</span>";
		
		// DEPRECATED, SHOULD CHANGED TO MEDIA.ALBUM
		if( $this->vars->imgsize && $img = $Object->media->get( "media.img" )) $result = "<img src='/public.img?id={$img->ID}&sze={$this->vars->imgsize}' alt='". $Object->get( "name" ) . "' />";
		
		$result .= "<p>". $Object->get( "content" ) ."</p>";
		
		if( $this->vars->link )
			$result .= "<a href='http://{$Object->get( "www" )}' target='_blank' class='link'>".translate( $this->vars->link )."</a>";
		
		return $result;
		
	}
	
	
	function filter_content() {
		global $DB, $Object;
		
		if( $this->vars->title )
			$this->name = $Object->get( 'name' );
			
		if( $this->vars->typetag )
			$result = "<span class='georgia type'>{$Object->type}</span>";
		
			$result .= "<p>". $Object->get( "content" ) ."</p>";
		
		if( $this->vars->link )
			$result .= "<a href='http://{$Object->get( "www" )}' target='_blank' class='link'>".translate( $this->vars->link )."</a>";
			
		if( $this->vars->customlink ) {
			$result .= "<a href='" . engine_get( 'url' ) . $this->vars->customlink . (( $this->vars->customget ) ? "?" . $this->vars->customget . "=" . $Object->get( 'ID' ) : "" ) . "' class='link'>";
			$result .= translate( $this->vars->linktext ) . "</a>";
		}
		
		return $result;
		
	}
	
	
	function filter_children() {
		global $DB, $Children, $ids;
	
		if( $Children ) {
		foreach( $Children as $Child ) {
		
			$title 		= '<span class="child-name">' . $Child->get( 'name' ) . '</span>';
			if( $Child->uri())
				$title  = '<a href="' . $Child->uri() . '" title="' . $Child->get( 'name' ) . '">' . $title . '</a>';
			
			if( $this->vars->sze ) {
				$img = (array) $Child->media->get( 'media.img' );
				$img = '<img src="/public.img?id=' . $img['ID'] . '&sze=' . $this->vars->sze . '" class="child-img" />';
				
				if( $Child->uri() )
					$img = '<a href="' . $Child->uri() . '" title="' . $Child->get( 'name' ) . '">' . $img . '</a>';
			
			}
				
			$content[] = '<li class="child">' . (( $img ) ? $img : '' ) . $title . '</li>';
			
		}
		return $content;
		}
	}
	
	
	function filter_stream() {
		global $DB, $Object;
		
		if( $streams = $Object->media->filter( 'media.stream' ))
			foreach( $streams as $stream ) {
				
				$meta = json_decode( $stream['meta'] );
				if( empty( $meta->source )) $meta->source = 'youtube';
			
				$this->vars->attr->src = "http://www.{$meta->source}.com/v/" . $meta->id;
				$SWF = new MEDIA();
			
				$result .= $SWF->_flash( $this->vars ) . "<br /><br />";
				
			}

		return $result;
	}
	
#	Create object link
	function filter_link() {
		global $DB, $Object;
		
		if( $link = $Object->get( "link" ))
			$result = "<a href='" . (( strpos( $link, "http" ) === 0 )? "" : "http://" ) . "$link' target='_blank'>$link</a>";

		return $result;
	}
	
#	Create ordered information schema
	function filter_keys() {
		global $DB, $Object;
		
		if( !$this->vars->keys )
			 $this->vars->keys = array( "name", "content", "tags" );

		
		foreach( $this->vars->keys as $key ) {
			
			if (	 $key == "background-image" )	$this->set_key_bgimage( $Object );
			else if( $key == "img" )				$result[] = $Object->media->render( $this->vars->mediatype, $this->vars->sze );
			else if( $key == "tags" )				$value = $this->render_tags();
			
			else if( count( $ksub = explode( ":", $key )) > 1 )	{
													$value = $this->{ "render_" . $ksub[ 0 ]}( $ksub[ 1 ], $ksub[ 2 ]);
													$key = str_replace( ":", "_", $key );
			
			} else									$value = (( strip_tags( $Object->get( $key )) != '<br>' )? $Object->get( $key ) : '' );
			
			if( $value )
				$result[] = "<span class='$key'>$value</span>"; unset( $value );
		}
		
		return implode( "\n", $result );
	}
	
		private function set_key_bgimage( $Obj ) {
			
			if( $id = $Obj->media->get_key( $this->vars->mediatype ))
				$this->set_attribute( "style", "background-image: url(/public.img?sze={$this->vars->sze}&id=$id);" );
		}
		
		function render_tags( $delimiter = ", " ) {
			global $Object;
			
			if( $tags = $Object->tags->filter())
				foreach( $tags as $Tag ) $value[] = $Tag->render();
				
			return $value? implode( $delimiter, $value ) : null;
			
		}
		
		function render_ref( $var ) {
			global $Object;
			
			if( $refs = $Object->ref_objects( $var ))
				foreach( $refs as $Obj ) $value .= "<a href='{$Obj->uri()}'>{$Obj->get('name')}</a>";
				
			return $value;
			
		}

		function render_text( $var, $column ) {
			global $Engine, $Object;
			
			if( $id = ( int ) $Object->get( $var ))
				return $Engine->text->get( $id, $column? "long": "short" );

		}
		

	

#	Render link to referred object
	function filter_referral() {
		global $DB, $Object;
		
		$ptype		= $this->vars->ptype;
		$pid		= $this->vars->pid;
		$objtype	= (( $Object->type ) ? $Object->type : $this->vars->objtype );
		$objid		= (( $Object->id ) ? $Object->id : $this->vars->objid ); 
		$tablestring= (( $this->vars->tablestring ) ? $this->vars->tablestring : 'object' );
		
		if( $links = $DB->reference( $ptype, $pid, $objtype, $objid, $tablestring )) {
			
			foreach( $links as $link ) {
				$Link	= new OBJECT( $ptype, (int) $link );
				$res[]  = '<li><a href="' . $Link->uri() . '" title="' . $Link->get( 'name' ) . '">' . $Link->get( 'name' ) . '</a></li>';
			}
			return $res;
		}
	}

#	Render meta
	function filter_meta() {
		global $DB, $Object;
		
		if( $this->vars->meta )
			foreach( $this->vars->meta as $key => $meta )
				$result[] = "<li><span>$key</span>{$Object->get( $meta )}</li>"; 

		return "<ul>" . implode( "\n", $result ) . "</ul>";
	}
	
	
	function filter_extended() {
		global $DB, $Object;
		
		$this->class = "extend";
		
		if( $imgs = $Object->media->filter( "media.img" ))
			foreach( $imgs as $img ) $return[] = "<img src='/public.img?id={$img['ID']}&sze=178.124' alt='". $Object->get( 'name' ) ."' rel='{$img['ID']}' />";
		
		$this->js = "
			obj = new ObjectSingle( 'extended' );
			obj.active_img( 0 );
		";
		
		$return[] = "<span class='content'><h1>". $Object->get( 'name' ) ."</h1>". $Object->get( 'content' ) ."</span>";
		
		return ( $return )? implode( "\n", $return ) : "Empty.";
	}
	
	
	function filter_tags() {
		global $DB, $Object;
		
		// How easy it is:
		// $id = $DB->set( "tag", array( "nameid"=> 60* ), false, true );
		// * op cms level kan je $Engine->text->set uitvoeren voor nieuwe text input, zie CORE.php (kan niet front-side)
		
		// $DB->set_reference ( "location", 77, "tag", $id, "tag" );
		// voorkomt intern duplicates.
		
		//$tags = $Object->tags->filter();
		
		//return "Object " . $Object->get( 'name' ) . "<br />tag: " . $tags[ 0 ]->get( "name" ) . " <a href='" . $tags[ 0 ]->uri() . "'>(link)</a>";
	}
	
	function ajax_keys() {
		
		return $this->filter_keys();
	}
	
	
	private function render_li( $cont, $class = false ) {
		
		if( $class ) $class = " class='$class'";
		
		return "<li$class>$cont</li>";
	}

} 

?>